package orders

import (
	"github.com/jinzhu/gorm"
	"time"
)

// CREATE TABLE `payment_transactions` (
//
//	`id` int NOT NULL AUTO_INCREMENT,
//	`transaction_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '支付系统生成的唯一流水号',
//	`user_id` bigint NOT NULL,
//	`amount` decimal(15,2) NOT NULL COMMENT '支付金额',
//	`payment_source` varchar(20) COLLATE utf8mb4_general_ci NOT NULL COMMENT '支付方式',
//	`order_id` bigint NOT NULL COMMENT '业务订单id',
//	`trade_no` varchar(64) COLLATE utf8mb4_general_ci NOT NULL COMMENT '业务订单编号',
//	`order_type` tinyint NOT NULL COMMENT '1-充值 2-云机 3-静态IP...',
//	`callback_data` json DEFAULT NULL COMMENT '第三方支付回调原始数据',
//	`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
//	`callback_time` datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP,
//	PRIMARY KEY (`id`) USING BTREE,
//	KEY `idx_user_order` (`user_id`,`order_id`,`order_type`)
//
// ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='支付交易核心记录';
type PaymentTransactions struct {
	Id            int64     `json:"id" gorm:"primary_key;column:id"`
	TransactionId string    `json:"transaction_id" gorm:"column:transaction_id"`
	UserId        string    `json:"user_id" gorm:"column:user_id"`
	Amount        float64   `json:"amount" gorm:"column:amount"`
	PaymentSource string    `json:"payment_source" gorm:"column:payment_source"`
	OrderId       int64     `json:"order_id" gorm:"column:order_id"`
	TradeNo       string    `json:"trade_no" gorm:"column:trade_no"`
	OrderType     int       `json:"order_type" gorm:"column:order_type"`
	CallbackData  string    `json:"callback_data" gorm:"column:callback_data"`
	CreatedAt     time.Time `json:"created_at" gorm:"column:created_at"`
	CallbackTime  time.Time `json:"callback_time" gorm:"column:callback_time"`
}

func (a *PaymentTransactions) TableName() string {
	return "payment_transactions"
}
func (a *PaymentTransactions) Create(db *gorm.DB) error {
	return db.Table(a.TableName()).Create(a).Error
}
func (a *PaymentTransactions) Update(db *gorm.DB) error {
	return db.Table(a.TableName()).Where("id = ?", a.Id).Updates(a).Error
}
func (a *PaymentTransactions) GetById(db *gorm.DB) error {
	return db.Table(a.TableName()).Where("id = ?", a.Id).First(a).Error
}
